/*
* Copyright (c) 2020-2023 Huawei Cloud Computing Technology Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
*    conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
*    of conditions and the following disclaimer in the documentation and/or other materials
*    provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
*    to endorse or promote products derived from this software without specific prior written
*    permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.huaweicloud.sdk.iot.device.demo;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import com.huaweicloud.sdk.iot.device.IoTDevice;

import java.nio.file.Files;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;


/**
 * This code is generated by FreeMarker
 *
 */
public class DeviceMain
{

    private static final String IOT_ROOT_CA_RES_PATH = "ca.jks";

    private static final String IOT_ROOT_CA_TMP_PATH = "huaweicloud-iotda-tmp-" + IOT_ROOT_CA_RES_PATH;

    public static void main(String args[]) throws InterruptedException, IOException {
        if (args.length < 3) {
            System.out.println("input parameters: serverUri deviceId secret");
            return;
        }

        // 从命令行获取设备参数
        String serverUri = "ssl://" + args[0] + ":8883";
        String deviceId = args[1];
        String secret = args[2];

        InputStream resource = IoTDevice.class.getClassLoader().getResourceAsStream(IOT_ROOT_CA_RES_PATH);
        File tmpCAFile = new File(IOT_ROOT_CA_TMP_PATH);
        Files.copy(resource, tmpCAFile.toPath(), REPLACE_EXISTING);

        // 创建设备
        IoTDevice device = new IoTDevice(serverUri, deviceId, secret, tmpCAFile);

        <#list device.serviceTypeCapabilities as service>
        ${service.serviceType}Service ${service.serviceType}ServiceInstance = new ${service.serviceType}Service();
        device.addService("${service.serviceId}", ${service.serviceType}ServiceInstance);
        ${service.serviceType}ServiceInstance.enableAutoReport(10000);

        </#list>

        if (device.init() != 0) {
            return;
        }
    }
}